package com.xiao.quicknote;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class NoteViewActivity extends Activity {

	private static final String TAG = "NoteViewActivity";

	private TextView mTextView;

	private String mNotePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_view);
		mTextView = (TextView) findViewById(R.id.textView);

		mNotePath = getIntent().getStringExtra("path");
		Log.v(TAG, mNotePath);

		if (PrefActivity.isAutoPlayMusic(this)) {
			playBgMusic();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		File noteDir = new File(mNotePath);
		File noteFile = new File(noteDir, noteDir.getName() + ".txt");
		NoteEditActivity.showNote(this, noteFile, mTextView, true);

	}

	private void playBgMusic() {
		File musicFile = null;
		File noteDir = new File(mNotePath);
		File[] noteDirSubFiles = noteDir.listFiles();
		for (File noteDirSubFile : noteDirSubFiles) {
			String fileName = noteDirSubFile.getName();
			if (fileName.endsWith(".mp3") || fileName.endsWith(".wma")
					|| fileName.endsWith(".mid")) {
				musicFile = noteDirSubFile;
				break;
			}
		}
		if (musicFile != null) {
			Intent playMusicIntent = new Intent(Intent.ACTION_VIEW);
			playMusicIntent.setDataAndType(Uri.fromFile(musicFile), "audio/*");
			startActivity(playMusicIntent);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		boolean result = super.onCreateOptionsMenu(menu);

		getMenuInflater().inflate(R.menu.activity_note_view, menu);

		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		boolean result = super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		case R.id.menu_new:
			Intent newNoteIntent = new Intent(this, NoteEditActivity.class);
			startActivity(newNoteIntent);
			break;
		case R.id.menu_delete:
			new AlertDialog.Builder(this)
					.setTitle(R.string.title_confirm_delete)
					.setPositiveButton(android.R.string.ok,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									NoteFileAdapter.deleteNote(mNotePath);
									finish();
								}
							}).setNegativeButton(android.R.string.cancel, null)
					.show();
			NoteFileAdapter.deleteNote(mNotePath);
			break;
		case R.id.menu_edit:
			Intent editNoteIntent = new Intent(this, NoteEditActivity.class);
			editNoteIntent.putExtra("path", mNotePath);
			startActivity(editNoteIntent);
			break;
		default:
			break;
		}

		return result;
	}
}
